/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ibrae.mchs.radiation.entities.controllers;

import ru.ibrae.mchs.radiation.entities.Selsovet;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ru.ibrae.mchs.radiation.entities.SubjectRf;
import ru.ibrae.mchs.radiation.entities.RajonOld;
import ru.ibrae.mchs.radiation.entities.Result;
import java.util.HashSet;
import java.util.Set;
import ru.ibrae.mchs.radiation.entities.Geoobject;

/**
 *
 * @author murr
 */
public class SelsovetJpaController {

    public SelsovetJpaController() {
        emf = Persistence.createEntityManagerFactory("DesktopApplication4PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Selsovet selsovet) {
        if (selsovet.getResultSet() == null) {
            selsovet.setResultSet(new HashSet<Result>());
        }
        if (selsovet.getGeoobjectSet() == null) {
            selsovet.setGeoobjectSet(new HashSet<Geoobject>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SubjectRf subjectRf = selsovet.getSubjectRf();
            if (subjectRf != null) {
                subjectRf = em.getReference(subjectRf.getClass(), subjectRf.getId());
                selsovet.setSubjectRf(subjectRf);
            }
            RajonOld rajonOld = selsovet.getRajonOld();
            if (rajonOld != null) {
                rajonOld = em.getReference(rajonOld.getClass(), rajonOld.getIdRajonO());
                selsovet.setRajonOld(rajonOld);
            }
            Set<Result> attachedResultSet = new HashSet<Result>();
            for (Result resultSetResultToAttach : selsovet.getResultSet()) {
                resultSetResultToAttach = em.getReference(resultSetResultToAttach.getClass(), resultSetResultToAttach.getIdRezult());
                attachedResultSet.add(resultSetResultToAttach);
            }
            selsovet.setResultSet(attachedResultSet);
            Set<Geoobject> attachedGeoobjectSet = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetGeoobjectToAttach : selsovet.getGeoobjectSet()) {
                geoobjectSetGeoobjectToAttach = em.getReference(geoobjectSetGeoobjectToAttach.getClass(), geoobjectSetGeoobjectToAttach.getIdNp());
                attachedGeoobjectSet.add(geoobjectSetGeoobjectToAttach);
            }
            selsovet.setGeoobjectSet(attachedGeoobjectSet);
            em.persist(selsovet);
            if (subjectRf != null) {
                subjectRf.getSelsovetSet().add(selsovet);
                subjectRf = em.merge(subjectRf);
            }
            if (rajonOld != null) {
                rajonOld.getSelsovetSet().add(selsovet);
                rajonOld = em.merge(rajonOld);
            }
            for (Result resultSetResult : selsovet.getResultSet()) {
                Selsovet oldSelsovetOfResultSetResult = resultSetResult.getSelsovet();
                resultSetResult.setSelsovet(selsovet);
                resultSetResult = em.merge(resultSetResult);
                if (oldSelsovetOfResultSetResult != null) {
                    oldSelsovetOfResultSetResult.getResultSet().remove(resultSetResult);
                    oldSelsovetOfResultSetResult = em.merge(oldSelsovetOfResultSetResult);
                }
            }
            for (Geoobject geoobjectSetGeoobject : selsovet.getGeoobjectSet()) {
                Selsovet oldSelsovetOfGeoobjectSetGeoobject = geoobjectSetGeoobject.getSelsovet();
                geoobjectSetGeoobject.setSelsovet(selsovet);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
                if (oldSelsovetOfGeoobjectSetGeoobject != null) {
                    oldSelsovetOfGeoobjectSetGeoobject.getGeoobjectSet().remove(geoobjectSetGeoobject);
                    oldSelsovetOfGeoobjectSetGeoobject = em.merge(oldSelsovetOfGeoobjectSetGeoobject);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Selsovet selsovet) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Selsovet persistentSelsovet = em.find(Selsovet.class, selsovet.getIdSelsov());
            SubjectRf subjectRfOld = persistentSelsovet.getSubjectRf();
            SubjectRf subjectRfNew = selsovet.getSubjectRf();
            RajonOld rajonOldOld = persistentSelsovet.getRajonOld();
            RajonOld rajonOldNew = selsovet.getRajonOld();
            Set<Result> resultSetOld = persistentSelsovet.getResultSet();
            Set<Result> resultSetNew = selsovet.getResultSet();
            Set<Geoobject> geoobjectSetOld = persistentSelsovet.getGeoobjectSet();
            Set<Geoobject> geoobjectSetNew = selsovet.getGeoobjectSet();
            if (subjectRfNew != null) {
                subjectRfNew = em.getReference(subjectRfNew.getClass(), subjectRfNew.getId());
                selsovet.setSubjectRf(subjectRfNew);
            }
            if (rajonOldNew != null) {
                rajonOldNew = em.getReference(rajonOldNew.getClass(), rajonOldNew.getIdRajonO());
                selsovet.setRajonOld(rajonOldNew);
            }
            Set<Result> attachedResultSetNew = new HashSet<Result>();
            for (Result resultSetNewResultToAttach : resultSetNew) {
                resultSetNewResultToAttach = em.getReference(resultSetNewResultToAttach.getClass(), resultSetNewResultToAttach.getIdRezult());
                attachedResultSetNew.add(resultSetNewResultToAttach);
            }
            resultSetNew = attachedResultSetNew;
            selsovet.setResultSet(resultSetNew);
            Set<Geoobject> attachedGeoobjectSetNew = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetNewGeoobjectToAttach : geoobjectSetNew) {
                geoobjectSetNewGeoobjectToAttach = em.getReference(geoobjectSetNewGeoobjectToAttach.getClass(), geoobjectSetNewGeoobjectToAttach.getIdNp());
                attachedGeoobjectSetNew.add(geoobjectSetNewGeoobjectToAttach);
            }
            geoobjectSetNew = attachedGeoobjectSetNew;
            selsovet.setGeoobjectSet(geoobjectSetNew);
            selsovet = em.merge(selsovet);
            if (subjectRfOld != null && !subjectRfOld.equals(subjectRfNew)) {
                subjectRfOld.getSelsovetSet().remove(selsovet);
                subjectRfOld = em.merge(subjectRfOld);
            }
            if (subjectRfNew != null && !subjectRfNew.equals(subjectRfOld)) {
                subjectRfNew.getSelsovetSet().add(selsovet);
                subjectRfNew = em.merge(subjectRfNew);
            }
            if (rajonOldOld != null && !rajonOldOld.equals(rajonOldNew)) {
                rajonOldOld.getSelsovetSet().remove(selsovet);
                rajonOldOld = em.merge(rajonOldOld);
            }
            if (rajonOldNew != null && !rajonOldNew.equals(rajonOldOld)) {
                rajonOldNew.getSelsovetSet().add(selsovet);
                rajonOldNew = em.merge(rajonOldNew);
            }
            for (Result resultSetOldResult : resultSetOld) {
                if (!resultSetNew.contains(resultSetOldResult)) {
                    resultSetOldResult.setSelsovet(null);
                    resultSetOldResult = em.merge(resultSetOldResult);
                }
            }
            for (Result resultSetNewResult : resultSetNew) {
                if (!resultSetOld.contains(resultSetNewResult)) {
                    Selsovet oldSelsovetOfResultSetNewResult = resultSetNewResult.getSelsovet();
                    resultSetNewResult.setSelsovet(selsovet);
                    resultSetNewResult = em.merge(resultSetNewResult);
                    if (oldSelsovetOfResultSetNewResult != null && !oldSelsovetOfResultSetNewResult.equals(selsovet)) {
                        oldSelsovetOfResultSetNewResult.getResultSet().remove(resultSetNewResult);
                        oldSelsovetOfResultSetNewResult = em.merge(oldSelsovetOfResultSetNewResult);
                    }
                }
            }
            for (Geoobject geoobjectSetOldGeoobject : geoobjectSetOld) {
                if (!geoobjectSetNew.contains(geoobjectSetOldGeoobject)) {
                    geoobjectSetOldGeoobject.setSelsovet(null);
                    geoobjectSetOldGeoobject = em.merge(geoobjectSetOldGeoobject);
                }
            }
            for (Geoobject geoobjectSetNewGeoobject : geoobjectSetNew) {
                if (!geoobjectSetOld.contains(geoobjectSetNewGeoobject)) {
                    Selsovet oldSelsovetOfGeoobjectSetNewGeoobject = geoobjectSetNewGeoobject.getSelsovet();
                    geoobjectSetNewGeoobject.setSelsovet(selsovet);
                    geoobjectSetNewGeoobject = em.merge(geoobjectSetNewGeoobject);
                    if (oldSelsovetOfGeoobjectSetNewGeoobject != null && !oldSelsovetOfGeoobjectSetNewGeoobject.equals(selsovet)) {
                        oldSelsovetOfGeoobjectSetNewGeoobject.getGeoobjectSet().remove(geoobjectSetNewGeoobject);
                        oldSelsovetOfGeoobjectSetNewGeoobject = em.merge(oldSelsovetOfGeoobjectSetNewGeoobject);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = selsovet.getIdSelsov();
                if (findSelsovet(id) == null) {
                    throw new NonexistentEntityException("The selsovet with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Selsovet selsovet;
            try {
                selsovet = em.getReference(Selsovet.class, id);
                selsovet.getIdSelsov();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The selsovet with id " + id + " no longer exists.", enfe);
            }
            SubjectRf subjectRf = selsovet.getSubjectRf();
            if (subjectRf != null) {
                subjectRf.getSelsovetSet().remove(selsovet);
                subjectRf = em.merge(subjectRf);
            }
            RajonOld rajonOld = selsovet.getRajonOld();
            if (rajonOld != null) {
                rajonOld.getSelsovetSet().remove(selsovet);
                rajonOld = em.merge(rajonOld);
            }
            Set<Result> resultSet = selsovet.getResultSet();
            for (Result resultSetResult : resultSet) {
                resultSetResult.setSelsovet(null);
                resultSetResult = em.merge(resultSetResult);
            }
            Set<Geoobject> geoobjectSet = selsovet.getGeoobjectSet();
            for (Geoobject geoobjectSetGeoobject : geoobjectSet) {
                geoobjectSetGeoobject.setSelsovet(null);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
            }
            em.remove(selsovet);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Selsovet> findSelsovetEntities() {
        return findSelsovetEntities(true, -1, -1);
    }

    public List<Selsovet> findSelsovetEntities(int maxResults, int firstResult) {
        return findSelsovetEntities(false, maxResults, firstResult);
    }

    private List<Selsovet> findSelsovetEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Selsovet.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Selsovet findSelsovet(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Selsovet.class, id);
        } finally {
            em.close();
        }
    }

    public int getSelsovetCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Selsovet> rt = cq.from(Selsovet.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
